﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="html" indent="yes"/>

    <xsl:param name="application" select="'{to do use this param application}'" />
    <xsl:param name="version" select="'{to do use this param version}'" />
    <xsl:param name="actionsTitle" select="'{to do use this param version}'" />
    <xsl:param name="startTime" select="'{to do use this param startTime}'" />
    <xsl:param name="endTime" select="'{to do use this param endTime}'" />
    <xsl:param name="duration" select="'{to do use this param duration}'" />

    <xsl:key name="component-by-name" match="//Components" use="@Name"/>


    <xsl:template match="/">
        <style>
            body
            {
            font-family: "Segoe UI Semibold","Segoe UI","Lucida Grande",Verdana,Arial,Helvetica,sans-serif;
            width:95%;
            }

            .layout
            {
            padding: 20px;
            width: 100%;
            background-color: #ffffff;
            }


            .HeaderDeploymentLog
            {
            color: #313131;
            font-size: 1.75em;
            font-weight: normal;
            margin: 0 0 12px;
            padding: 0;
            }

            .Title
            {
            color: #260859;
            font-size: 1.25em;
            font-weight: normal;
            margin: 20px 0 20px;
            padding: 3 3 3 3;
            background-color: #C0C0C0;
            }


            table th {
            color: #ffffff;
            background-color: #555555;
            border: 1px solid #555555;
            padding: 3px;
            vertical-align: top;
            text-align: center;
            }

            Table
            {
            font-family: verdana,helvetica,arial,sans-serif;
            color: #555555;
            font-size: 16px;
            text-align: left;
            border: 1px solid #555555;
            border-collapse: collapse;
            }

            table td
            {
            padding: 5px;
            }
            td.columnStatus
            {
            text-align: center;
            }

            .ServerDetailTitle
            {
            color: #260859;
            font-size: 1.1em;
            font-weight: normal;
            margin: 20px 0 20px;
            padding: 3 3 3 3;
            width: 100%;
            border-bottom: 1px solid #260859;
            }

            .ComponentDetailTitle
            {
            color: #260859;
            font-size: 1.1em;
            font-weight: bold;
            }

            .DeploymentActions
            {
            font-weight: bold;
            }

            .ResultDetail
            {
            width: 100%;
            margin-bottom: 20px;
            }

            .ResultDetailTitle
            {
            width:100%;
            color: #260859;
            font-size: 1em;
            font-weight: normal;
            padding: 3 3 3 3;
            background-color: #C0C0C0;
            display: inline-block;
            border: 1px solid #260859;
            }


            .ResultsTable
            {
            width:100%;
            border: 1px solid #260859;
            }

            table.ResultDetail tr:nth-child(even)
            {
            background: #99FFFF;
            }
            table.ResultDetail tr:nth-child(odd)
            {
            background: #FFFFFF;
            }

            .outputCommand
            {
            color: #260859;
            }
            .attributeName, .attributeSeparator
            {
            font-weight:bold;
            }


            .success
            {
                color: #00FF00;
            }

            .Failure
            {
                color: #FF0000;
            }


        </style>

        <body>

            <div class="HeaderDeploymentLog">
                <span>
                    Application Management log for <xsl:value-of select="$application"/> V <xsl:value-of select="$version"/>
                </span>
            </div>

            <div class="layout">

                <div class="Title">
                    <span><xsl:value-of select="$actionsTitle"/> Summary</span>
                </div>

                <div>
                    <span class="attributeName">Start Time : </span>
                    <xsl:value-of select="$startTime"/>
                    <br/>
                    <span class="attributeName">End Time : </span>
                    <xsl:value-of select="$endTime"/>
                    <br/>
                    <span class="attributeName">Duration : </span>
                    <xsl:value-of select="$duration"/>
                    <br/>
                    <br/>
                </div>

                <table border="1">
                    <th></th>
                    <!-- Do the table Header-->
                    <xsl:apply-templates select="//Server" mode="ServerSummaryTitle" />
                    <!-- Browse components and push deployment state for the server -->
                    <xsl:for-each select="//Components[generate-id() = generate-id(key('component-by-name',@Name)[1])]">
                        <xsl:call-template name="ComponentsStatePerServer">
                            <xsl:with-param name="componentName" select="@Name" />
                        </xsl:call-template>
                    </xsl:for-each>
                </table>

                <div class="Title">
                    <span>Deployment Detail</span>
                </div>

                <!-- Do the server detail-->
                <xsl:apply-templates select="//Server" mode="ServerDetail" />

            </div>
        </body>
    </xsl:template>


    <!-- Begin : Summary Coloumns table title -->
    <xsl:template match="//Server" mode="ServerSummaryTitle">
        <th>
            <xsl:value-of disable-output-escaping="yes" select="@Name"/>
            <br/>
            (<xsl:value-of select="@HostName"/>)
        </th>
    </xsl:template>
    <!-- End : Summary Coloumns table title -->


    <!-- Begin : Summary Components State per Server -->
    <xsl:template name="ComponentsStatePerServer">
        <xsl:param name="componentName" />
        <tr>
            <td>
                <xsl:value-of select="$componentName"/>
            </td>
            <!-- Browse each server and test if this component has actions -->
            <xsl:apply-templates select="//Server" mode="ServerTableContent">
                <xsl:with-param name="componentName" select="$componentName" />
            </xsl:apply-templates>
        </tr>
    </xsl:template>
    <!-- End : Summary Components State per Server -->

    <!-- Begin : Components State -->
    <xsl:template match="//Server" mode="ServerTableContent" >
        <xsl:param name="componentName" />
            <xsl:choose>
                <xsl:when test="count(Components[@Name=$componentName]/DeploymentActions) = 0">
                    <td class="columnStatus"></td>
                </xsl:when>
                <xsl:when test="count(Components[@Name=$componentName]/DeploymentActions[@Status='False']) > 0">
                    <td class="columnStatus failure">
                        KO
                    </td>
                </xsl:when>
                <xsl:otherwise>
                    <td class="columnStatus success">
                        OK
                    </td>
                </xsl:otherwise>
            </xsl:choose>
    </xsl:template>
    <!-- End : Components State -->

    <xsl:template match="//Server" mode="ServerDetail">

        <div class="ServerDetailTitle">
            <xsl:value-of select="@Name"/> (<xsl:value-of select="@HostName"/>)
        </div>

        <ul>
            <xsl:apply-templates select="Components" mode="ComponentDetail" />
        </ul>



    </xsl:template>


    <xsl:template match="//Components" mode="ComponentDetail">

        <li>
            <span class="ComponentDetailTitle">
                <xsl:value-of select="@ComponentType"/> : <xsl:value-of select="@componentName"/> (<xsl:value-of select="@Name"/>)
            </span>

            <xsl:apply-templates select="DeploymentActions" />

        </li>


    </xsl:template>


    <xsl:template match="//DeploymentActions">

        <div class="DeploymentActions">
            Step <xsl:value-of select="@DeploymentStep"/> : <xsl:value-of select="@Status"/>
        </div>

        <ul>
            <xsl:apply-templates select="Results" />
        </ul>

    </xsl:template>


    <xsl:template match="Results">
        <li>
            <xsl:value-of select="@Command"/> : Status = <xsl:value-of select="@Status"/>

            <table border="1" class="ResultDetail">
                <tr>
                    <th>Status</th>
                    <th>Start</th>
                    <th>End</th>
                    <th>Duration</th>
                </tr>
                <tr>
                    <td>
                        <xsl:value-of select="@Status"/>
                    </td>
                    <td>
                        <xsl:value-of select="@Start"/>
                    </td>
                    <td>
                        <xsl:value-of select="@End"/>
                    </td>
                    <td>
                        <xsl:value-of select="@Duration"/>
                    </td>
                </tr>

                <tr>
                    <td colspan="4" class="ResultDetailTitle">
                        Output
                    </td>
                </tr>

                <xsl:if test="@Status='False'">
                    <tr>
                        <td colspan="4" class="outputCommand">
                            Exit code : <xsl:value-of select="@ExitCode"/>
                            Exception : <xsl:value-of select="@Exception"/>
                        </td>
                    </tr>
                </xsl:if>


                <xsl:choose>
                    <xsl:when test="count(detailedResultLine) > 0">
                        <tr>
                            <td colspan="4" class="outputCommand">
                                <xsl:apply-templates select="detailedResultLine"  />
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="not(@Result) or (@Result and @Result='')">
                        <tr>
                            <td colspan="4" class="outputCommand">
                                <xsl:text>Command without output</xsl:text>
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="@Result">
                        <tr>
                            <td colspan="4" class="outputCommand">
                                <xsl:apply-templates select="@Result"  />
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="count(Result) = 1">
                        <tr>
                            <td colspan="4" class="outputCommand">
                                <xsl:apply-templates select="Result/@*"  />
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="Result" />
                    </xsl:otherwise>
                </xsl:choose>
            </table>
        </li>
    </xsl:template>

    <xsl:template match="detailedResultLine">
        <xsl:value-of select="text()" />
        <br/>
    </xsl:template>

    <xsl:template match="Result">
        <tr>
            <td colspan="4">
                <xsl:apply-templates select="@*" />
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="@*">
        <span>
            <span class="attributeName">
                <xsl:value-of select="name()"/> :
            </span>
            <xsl:value-of select="."/>
        </span>
        <span class="attributeSeparator">
            |
        </span>
    </xsl:template>

</xsl:stylesheet>
